package com.chinaunicom.club.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinaunicom.club.model.Activity;
import com.chinaunicom.club.service.IActivity;

@Controller
@RequestMapping("/club/activity")
public class ActivityController {
	
	@Resource
	public IActivity service;
	
	/**
	 * 查询所有的活动信息
	 * @param model
	 * @return
	 */
	@RequestMapping("/queryAll")
	public String queryAll(Model model){
		//1.从数据库中取出所有的活动信息
		List<Activity> activities = service.queryAll(null);		
		//2.将活动信息传递到jsp页面
		model.addAttribute("activities", activities);
		return "/WEB-INF/club/activity/list.jsp";
		//return activities;
	}

	/**
	 * 返回所有活动信息的json格式
	 * @param model
	 * @return
	 */
	@RequestMapping("/jsonQueryAll")
	@ResponseBody
	public Map<String,Object> jsonQueryAll(Model model,int page,int rows){
		//1.从数据库中统计总的条数
		int total = service.count(null);
		//2.从数据库中取出所有分页好的活动信息
		List<Activity> activities = service.pageQueryAll(page,rows);		
		//3.将活动信息传递到jsp页面，构造成EASYUI要求接收到的JSON格式
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", activities);
		return map;
	}
	
	/**
	 * 查看单个活动信息
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/viewDetail")
	public String viewDetail(Model model,int id){
		//1.从数据库中取出该ID的活动信息
		Activity activity = service.selectByPrimaryKey(id);
		//2.将活动信息传递到JSP页面
		model.addAttribute("activity", activity);
		return "/WEB-INF/club/activity/viewDetail.jsp";
	}
	
	/**
	 * 跳转到添加活动信息页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/toAdd")
	public String toAdd(Model model){
		return "/WEB-INF/club/activity/toAdd.jsp";
	}

	/**
	 * 保存新增的活动信息到数据库
	 * @param model
	 * @param activity
	 * @return
	 */
	@RequestMapping("/add")
	public String add(Model model,Activity activity){
		service.insertSelective(activity);
		return "/club/activity/queryAll";
	}
	
	
	/**
	 * 跳转到修改活动信息页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/toUpdate")
	public String toUpdate(Model model,int id){
		//1.从数据库中取出该ID的活动信息
		Activity activity = service.selectByPrimaryKey(id);
		//2.将活动信息传递到JSP页面
		model.addAttribute("activity", activity);
		return "/WEB-INF/club/activity/toUpdate.jsp";
	}
	
	/**
	 * 保存修改后的活动信息到数据库
	 * @param model
	 * @param activity
	 * @return
	 */
	@RequestMapping("/save")
	public String save(Model model,Activity activity){
		service.updateByPrimaryKeySelective(activity);
		return "/club/activity/queryAll";
	}
	
	/**
	 * 将该活动进行隐式删除
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/del")
	public String del(Model model,int id){
		Activity activity = new Activity();
		activity.setId(id);
		activity.setIsDel(0);
		service.updateByPrimaryKeySelective(activity);
		return "/club/activity/queryAll";
	}
	
	/**
	 * 跳转到审核活动信息的页面
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/toCheck")
	public String toCheck(Model model,int id){
		Activity activity = service.selectByPrimaryKey(id);
		model.addAttribute("activity", activity);
		return "/WEB-INF/club/activity/toCheck.jsp";
	}
	
	/**
	 * 根据审核结果，修改活动信息的审核状态
	 * @param model
	 * @param id
	 * @param checkStatus
	 * @return
	 */
	@RequestMapping("/check")
	public String Check(Model model,int id,int checkStatus){
		service.check(id,checkStatus);
		return "/club/activity/queryAll";
	}
	
	/**
	 * 导出所有的活动信息
	 * @param request
	 * @param response
	 */
	@RequestMapping("/exportAllActivities")
	public void exportAllActivities(HttpServletRequest request,HttpServletResponse response){
		//1.调用方法，将活动信息填充到excel表格中，并提供下载
		service.exportAllActivities(request,response);
	}
	
	/**
	 * 导出单个活动信息
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/exportActivityById")
	public void exportActivityById(int id,HttpServletRequest request,HttpServletResponse response){
		boolean flag = service.exportActivityById(id,request,response);
	}
}
